#include "dbpagewidget.h"

DBPageWidget::DBPageWidget(QObject *parent) : QObject(parent)
{
    //控件初始化
    m_tableView = nullptr;
    m_btnPrevPage = nullptr;
    m_btnNextPage = nullptr;
    m_btnFirstPage = nullptr;
    m_btnLastPage = nullptr;

    m_labelPageInfo = nullptr;
    m_labelCurrentPageIdx = nullptr;
    m_labelTotalPages = nullptr;
    m_labelTotalNotes = nullptr;
    m_labelCurrentNotes = nullptr;
    m_labelNotesInPerPage = nullptr;

    //变量初始化
    m_dbCommName = QLatin1String("qt_sql_default_connection");   //设置数据库连接名称
    m_dbTableName = QLatin1String("*");  //数据库表名称
    m_dbColumnsSql = QLatin1String("*"); //查询列名称
    m_dbOrderSql = QLatin1String("");   //排序条件
    m_dbWhereSql = QLatin1String("");   //查询条件

    m_iNotesPerPage = 20;    //每页显示多少条数据

    //私有变量
    m_iTotalNotes = 0;       //共xx条数据
    m_iTotalPages = 0;       //共xx页
    m_iCurrentPage = 0;      //当前为第xx页

    //查询数据条数
    m_pDbNotesCount = new DbNotesCount();
    connect(this, SIGNAL(queryNotesCount(const QString)), m_pDbNotesCount, SLOT(query(const QString)));
    connect(m_pDbNotesCount, SIGNAL(dbNotesCountQFinished(int, double)),
            this, SLOT(dbNoteCount(int, double)));

    m_pSqlModel = new MySqlQuery(this);
}

void DBPageWidget::setTableView(QTableView *tableView)
{
    m_tableView = tableView;
}

void DBPageWidget::setNavButton(QAbstractButton *btnPrevPage, QAbstractButton *btnNextPage, QAbstractButton *btnFirstPage, QAbstractButton *btnLastPage)
{
    setBtnPrevPage(btnPrevPage);
    setBtnNextPage(btnNextPage);
    setBtnFirstPage(btnFirstPage);
    setBtnLastPage(btnLastPage);
}

void DBPageWidget::setBtnPrevPage(QAbstractButton *btnPrevPage)
{
    m_btnPrevPage = btnPrevPage;
    if(m_btnPrevPage != nullptr)
    {
        connect(m_btnPrevPage, SIGNAL(clicked()), this, SLOT(showPrevPage()));
    }
}

void DBPageWidget::setBtnNextPage(QAbstractButton *btnNextPage)
{
    m_btnNextPage = btnNextPage;
    if(m_btnNextPage != nullptr)
    {
        connect(m_btnNextPage, SIGNAL(clicked()), this, SLOT(showNextPage()));
    }

}

void DBPageWidget::setBtnFirstPage(QAbstractButton *btnFirstPage)
{
    m_btnFirstPage = btnFirstPage;
    if(m_btnFirstPage != nullptr)
    {
        connect(m_btnFirstPage, SIGNAL(clicked()), this, SLOT(showFirstPage()));
    }
}

void DBPageWidget::setBtnLastPage(QAbstractButton *btnLastPage)
{
    m_btnLastPage = btnLastPage;
    if(m_btnLastPage != nullptr)
    {
        connect(m_btnLastPage, SIGNAL(clicked()), this, SLOT(showLastPage()));
    }
}

void DBPageWidget::setLabelPageInfo(QLabel *labelPageInfo)
{
    m_labelPageInfo = labelPageInfo;
}

void DBPageWidget::setLabelCurrentPageIdx(QLabel *labelCurrentPageIdx)
{
    m_labelCurrentPageIdx = labelCurrentPageIdx;
}

void DBPageWidget::setLabelTotalPages(QLabel *labelTotalPages)
{
    m_labelTotalPages = labelTotalPages;
}

void DBPageWidget::setLabelTotalNotes(QLabel *labelTotalNotes)
{
    m_labelTotalNotes = labelTotalNotes;
}

void DBPageWidget::setLabelCurrentNotes(QLabel *labelCurrentNotes)
{
    m_labelCurrentNotes = labelCurrentNotes;
}

void DBPageWidget::setLabelNotesInPerPage(QLabel *labelNotesInPerPage)
{
    m_labelNotesInPerPage = labelNotesInPerPage;
}

void DBPageWidget::setDbInfo(const QString &dbFileName, const QString &dbCommName)
{
    setDbFileName(dbFileName);
    setDbCommName(dbCommName);
}

void DBPageWidget::setDbQueryInfo(const QString &dbTableName, const QString &dbColumnsSql, const QString &dbWhereSql, const QString &dbOrderSql)
{
    setDbTableName(dbTableName);
    setDbColumnsSql(dbColumnsSql);
    setDbWhereSql(dbWhereSql);
    setDbOrderSql(dbOrderSql);
}

void DBPageWidget::setDbFileName(const QString &dbFileName)
{
    m_pDbNotesCount->setDbFileName(dbFileName);
}

QString DBPageWidget::dbCommName() const
{
    return m_dbCommName;
}

void DBPageWidget::setDbCommName(const QString &dbCommName)
{
    m_dbCommName = dbCommName;
    m_pDbNotesCount->setDbCommName(m_dbCommName + "_cnt_in_thread");  //设置计数类的信号和槽
}

void DBPageWidget::setDbTableName(const QString &dbTableName)
{
    m_dbTableName = dbTableName;
}

void DBPageWidget::setDbColumnsSql(const QString &dbColumnsSql)
{
    m_dbColumnsSql = dbColumnsSql;
}

void DBPageWidget::setDbOrderSql(const QString &dbOrderSql)
{
    m_dbOrderSql = dbOrderSql;
}

void DBPageWidget::setDbWhereSql(const QString &dbWhereSql)
{
    m_dbWhereSql = dbWhereSql;
}

void DBPageWidget::setINotesPerPage(int iNotesPerPage)
{
    m_iNotesPerPage = iNotesPerPage;
}

void DBPageWidget::setColumnNamesList(const QList<QString> &ColumnNamesList)
{
    m_ColumnNamesList = ColumnNamesList;
}

void DBPageWidget::setColumnWidthList(const QList<int> &ColumnWidthList)
{
    m_ColumnWidthList = ColumnWidthList;
}

void DBPageWidget::init()
{

}

// **执行查询操作
void DBPageWidget::query()
{
    //设置按钮为无效状态
    setBtnFirstPageEnable(false);
    setBtnPrevPageEnable(false);
    setBtnNextPageEnable(false);
    setBtnLastPageEnable(false);

    //查询当前共多少条数据m_dbCountName
    m_sqlTemp = QString("select count(%1) from %2 %3;")
            .arg(m_dbCountName).arg(m_dbTableName).arg(m_dbWhereSql);

    emit queryNotesCount(m_sqlTemp);
}

// ** 显示前一页内容
void DBPageWidget::showPrevPage()
{
    if(m_iCurrentPage > 1)
    {
        m_iCurrentPage--;
    }

    showDataByPage(m_iCurrentPage);
    changeButtonState();
}

// ** 显示下一页的内容
void DBPageWidget::showNextPage()
{
    if(m_iCurrentPage < m_iTotalPages)
    {
        m_iCurrentPage++;
    }

    showDataByPage(m_iCurrentPage);
    changeButtonState();

}

// ** 显示第一页的内容
void DBPageWidget::showFirstPage()
{
    m_iCurrentPage = 1;
    showDataByPage(m_iCurrentPage);
    changeButtonState();
}

// ** 显示最后一页的内容
void DBPageWidget::showLastPage()
{
    m_iCurrentPage = m_iTotalPages;
    showDataByPage(m_iCurrentPage);
    changeButtonState();
}

// 获得当前共多少条的查询结果
void DBPageWidget::dbNoteCount(int count, double msec)
{
    Q_UNUSED(msec)

    //设置总的数据条数 和 总的页数
    m_iTotalNotes = count;
    if(0 == (m_iTotalNotes % m_iNotesPerPage))
    {
        m_iTotalPages = m_iTotalNotes/m_iNotesPerPage;
    }
    else
    {
        m_iTotalPages = m_iTotalNotes/m_iNotesPerPage + 1;
    }

    showFirstPage();
}

// 显示数据库内容
void DBPageWidget::showData(QString sql)
{
    //查询并显示到tableView
    m_pSqlModel->setQuery(sql, QSqlDatabase::database(m_dbCommName));
    m_tableView->setModel(m_pSqlModel);

    // 设置tableview的表头和宽度
    int iModelColumnCount = m_pSqlModel->columnCount();
    int iNameColumnCount = m_ColumnNamesList.size();
    int n = (iModelColumnCount > iNameColumnCount)?iNameColumnCount:iModelColumnCount;

    for(int i=0; i<n; i++)
    {
        m_pSqlModel->setHeaderData(i, Qt::Horizontal, m_ColumnNamesList.at(i));
        m_tableView->setColumnWidth(i, m_ColumnWidthList.at(i));
    }

    // 设置相关信息
    setPagesInfo();
}

void DBPageWidget::showDataByPage(int iPage)
{
    //起始条数
    int iNoteStart = (iPage-1)*m_iNotesPerPage;
    m_sqlTemp = QString("select %1 from %2 %3 order by %4")
            .arg(m_dbColumnsSql)
            .arg(m_dbTableName)
            .arg(m_dbWhereSql)
            .arg(m_dbOrderSql);

    m_sqlString = QString("%1 limit %2,%3;").arg(m_sqlTemp).arg(iNoteStart).arg(m_iNotesPerPage);
    showData(m_sqlString);
}

//设置相关显示信息
void DBPageWidget::setPagesInfo()
{
    // 当前页 信息
    if(nullptr != m_labelPageInfo)
    {
        m_labelPageInfo->setText( QString(tr("【第 %1 / %2 页】"))
                    .arg(m_iCurrentPage)
                    .arg(m_iTotalPages) );
    }

    // 共多少页
    if(nullptr != m_labelTotalPages)
    {
        m_labelTotalPages->setText( QString(tr("共 %1 页")).arg(m_iTotalPages) );
    }

    //第多少页
    if(nullptr != m_labelCurrentPageIdx)
    {
        m_labelCurrentPageIdx->setText( QString(tr("第 %1 页")).arg(m_iCurrentPage));
    }

    //共多少条
    if(nullptr != m_labelTotalNotes)
    {
        m_labelTotalNotes->setText( QString(tr("共 %1 条")).arg(m_iTotalNotes) );
    }

    //当前第多少条
    if(nullptr != m_labelCurrentNotes)
    {
        m_labelCurrentNotes->setText( QString(tr("第 %1~%2 条"))
                                      .arg((m_iCurrentPage-1)*m_iNotesPerPage+1)
                                      .arg(m_iCurrentPage*m_iNotesPerPage));
    }

    if(nullptr != m_labelNotesInPerPage)
    {
        m_labelNotesInPerPage->setText( QString(tr("每页 %1 条")).arg(m_iNotesPerPage) );
    }
}

void DBPageWidget::changeButtonState()
{
    if(m_iCurrentPage > 1)
    {
        setBtnFirstPageEnable(true);
        setBtnPrevPageEnable(true);
    }

    if(m_iCurrentPage < m_iTotalPages)
    {
        setBtnNextPageEnable(true);
        setBtnLastPageEnable(true);
    }

    if(m_iTotalPages <= 1)
    {
        setBtnFirstPageEnable(false);
        setBtnPrevPageEnable(false);
        setBtnNextPageEnable(false);
        setBtnLastPageEnable(false);
    }
}

//设置前一页按钮的状态
inline void DBPageWidget::setBtnPrevPageEnable(bool bEnabled)
{
    if(nullptr != m_btnPrevPage)
    {
        m_btnPrevPage->setEnabled(bEnabled);
    }
}
//设置后一页按钮的状态
inline void DBPageWidget::setBtnNextPageEnable(bool bEnabled)
{
    if(nullptr != m_btnNextPage)
    {
        m_btnNextPage->setEnabled(bEnabled);
    }
}
//设置第一页按钮的状态
inline void DBPageWidget::setBtnFirstPageEnable(bool bEnabled)
{
    if(nullptr != m_btnFirstPage)
    {
        m_btnFirstPage->setEnabled(bEnabled);
    }
}
//设置最后一页按钮的状态
inline void DBPageWidget::setBtnLastPageEnable(bool bEnabled)
{
    if(nullptr != m_btnLastPage)
    {
        m_btnLastPage->setEnabled(bEnabled);
    }
}

void DBPageWidget::setDbCountName(const QString &dbCountName)
{
    m_dbCountName = dbCountName;
}
